package com.jiandan.business.web.selfservice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiandan.business.entity.selfservice.CemeteryRegion;
import com.jiandan.business.entity.selfservice.Machine;
import com.jiandan.business.entity.selfservice.People;
import com.jiandan.business.entity.selfservice.QueryParam;
import com.jiandan.business.service.selfservice.SelfService;
import com.jiandan.business.util.selfservice.SelfServiceConstants;

// TODO: Auto-generated Javadoc
/**
 * 触摸机终端所有请求处理.
 */
@Controller
@RequestMapping(value = "/selfService")
public class SelfServiceController {

	/** The logger. */
	private static Logger logger = Logger
			.getLogger(SelfServiceController.class);

	/** 对应Service. */
	@Autowired
	private SelfService selfService;

	/** The session. */
	@Autowired
	private HttpSession session;

	/** The request. */
	@Autowired
	private HttpServletRequest request;

	/**
	 * 所有机器列表（AJAX）.
	 * 
	 * @return the all mechines
	 */
	@RequestMapping(value = "getAllMachines")
	@ResponseBody
	public Object getAllMechines() {
		List<Machine> machineList = selfService.getAllMachines();
		JSONObject jsonObj = new JSONObject();
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_RESULT,
				SelfServiceConstants.ResponseAttributes.JSON_RESULT_SUCCESS);
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_DATA,
				machineList);
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if ("x".equals(arg1) || "y".equals(arg1)||"password".equals(arg1)) {
					return true;
				}
				return false;
			}
		});
		String resData = JSONObject.fromObject(jsonObj, config).toString();
		logger.debug(resData);
		return resData;
	}

	/**
	 * 登陆.
	 * 
	 * @param id
	 *            the id
	 * @param password
	 *            the password
	 * @return the string
	 */
	@RequestMapping(value = "login")
	public String login(Integer id, String password) {
		Machine machine = selfService.getMachines(id);
		if (machine != null && machine.getPassword().equals(password)) {
			session.setAttribute(
					SelfServiceConstants.SessionAttributes.MACHINE, machine);
			request.setAttribute(
					SelfServiceConstants.RequestAttributes.MACHINE, machine);
			return "main";
		} else if (machine == null) {
			request.setAttribute(
					SelfServiceConstants.RequestAttributes.LOGIN_ERROR,
					SelfServiceConstants.Messages.NO_MACHINE);
			return "login";
		} else if (!machine.getPassword().equals(password)) {
			request.setAttribute(
					SelfServiceConstants.RequestAttributes.LOGIN_ERROR,
					SelfServiceConstants.Messages.PASSWORD_ERROR);
			return "login";
		}
		request.setAttribute(
				SelfServiceConstants.RequestAttributes.LOGIN_ERROR,
				SelfServiceConstants.Messages.LOGIN_ERROR);
		return "login";
	}

	/**
	 * 根据拼音联想汉字.
	 * 
	 * @param pinyin
	 *            the pinyin
	 * @return the object
	 */
	@RequestMapping(value = "pinyinAssociate")
	@ResponseBody
	public Object pinyinAssociate(String pinyin) {
		List<String> chinese = selfService.pinyinAssociate(pinyin);
		JSONObject jsonObj = new JSONObject();
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_RESULT,
				SelfServiceConstants.ResponseAttributes.JSON_RESULT_SUCCESS);
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_DATA,
				chinese);
		String resData = JSONObject.fromObject(jsonObj).toString();
		logger.debug(resData);
		return resData;
	}

	/**
	 * Gets the all region names.
	 * 
	 * @return the all region names
	 */
	@RequestMapping(value = "getAllRegionNames")
	@ResponseBody
	public Object getAllRegionNames() {
		List<CemeteryRegion> allRegionNames = selfService.getAllRegionNames();
		JSONObject jsonObj = new JSONObject();
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_RESULT,
				SelfServiceConstants.ResponseAttributes.JSON_RESULT_SUCCESS);
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_DATA,
				allRegionNames);
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if ("rows".equals(arg1)) {
					return true;
				}
				return false;
			}
		});
		String resData = JSONObject.fromObject(jsonObj, config).toString();
		logger.debug(resData);
		return resData;
	}

	@RequestMapping(value = "getOneRegionDetail")
	@ResponseBody
	public Object getOneRegionDetail(Integer regionId) {
		CemeteryRegion region = selfService.getOneRegionDetail(regionId);
		JSONObject jsonObj = new JSONObject();
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_RESULT,
				SelfServiceConstants.ResponseAttributes.JSON_RESULT_SUCCESS);
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_DATA,
				region);
		String resData = JSONObject.fromObject(jsonObj).toString();
		logger.debug(resData);
		return resData;

	}
	
	@RequestMapping(value = "findPeoples")
	@ResponseBody
	public Object getFindPeoples(QueryParam param) {
		List<People> peoples = selfService.findPeoples(param);
		JSONObject jsonObj = new JSONObject();
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_RESULT,
				SelfServiceConstants.ResponseAttributes.JSON_RESULT_SUCCESS);
		jsonObj.put(SelfServiceConstants.ResponseAttributes.JSON_KEY_DATA,
				peoples);
		String resData = JSONObject.fromObject(jsonObj).toString();
		logger.debug(resData);
		return resData;

	}

}
